<?php

namespace App\Jobs;

use App\Models\ModelNumberSpecification;
use App\Models\ProductCenter;
use App\Models\ProductModelNumber;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class BatchAddJobTypePrice implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    // 任务可以尝试的最大次数。
    public $tries = 2;

    // 任务可以执行的最大秒数 (超时时间)。
    public $timeout = 600;

    private $data;

    /**
     * Create a new job instance.
     *
     * php artisan queue:work redis --queue=batchAddJobTypePrice
     *
     * @return void
     */
    public function __construct($data)
    {
        //
        $this->queue = 'batchAddJobTypePrice';
        $this->data = $data;
    }


    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        Log::channel('joblog')->info('--------------------------请求日志队列开始--------------------------');
        Log::channel('joblog')->info('开始时间：'.date("Y-m-d H:i:s"));
        $this->data['is_update'] = $this->data['is_update']??1;
        $job_type_price_arr = $this->data['job_type_price_arr'];
        foreach($job_type_price_arr as $k=>$v){
            foreach($this->data['product_ids'] as $kk=>$vv){
                foreach($this->data['model_number_ids'] as $kkk=>$vvv){
                    if(ProductModelNumber::where('product_id',$vv)->where('model_number_id',$vvv)->first()){
                        foreach($this->data['specification_ids'] as $key=>$val){
                            $res = ModelNumberSpecification::where('product_id',$vv)
                                ->where('model_number_id',$vvv)
                                ->where('specification_id',$val)
                                ->first();
                            if($res){
                                $where =
                                    [
                                        'product_id'=>$vv,
                                        'model_number_id'=>$vvv,
                                        'specification_id'=>$val,
                                        'job_type_id'=>$v['job_type_id']
                                    ];
                                ProductCenter::updateOrCreate(
                                    $where,
                                    [$where,'price'=>$v['price']]);
                            }
                        }
                    }
                }
            }
        }
        Log::channel('joblog')->info('队列名称:'.$this->queue);
        Log::channel('joblog')->info('请求地址:'.$this->data['path']);
        Log::channel('joblog')->info('请求方式:'.$this->data['method']);
        Log::channel('joblog')->info('请求参数:'.json_encode($this->data ,JSON_UNESCAPED_UNICODE));
        Log::channel('joblog')->info('结束时间：'.date("Y-m-d H:i:s"));
        Log::channel('joblog')->info('--------------------------请求日志队列结束--------------------------');
    }
}
